/**
 * Created by ChenYong on 2017-04-12.
 *
 * 用户列表。
 */

$(function () {
    /** 姓名的 jQuery 对象 */
    var $loginName = $("#login_name");
    /** 所属行业的 jQuery 对象 */
    var $industryCode = $("#industry_code");
    /** 用户类型的 jQuery 对象 */
    var $isAdmin = $("#is_admin");
    /** 是否为领导的 jQuery 对象 */
    var $isLeader = $("#is_leader");
    /** 搜索按钮的 jQuery 对象 */
    var $searchUserBtn = $("#search_user_btn");
    /** 用户列表的表格，DataTables 的 API 对象 */
    var userTable = null;

    // 监听所属行业下拉列表搜索
    $industryCode.change(function() {
        searchData();
    });

    // 监听用户类型下拉列表搜索
    $isAdmin.change(function() {
        searchData();
    });

    // 监听是否为领导下拉列表搜索
    $isLeader.change(function() {
        searchData();
    });

    // 点击搜索按钮
    $searchUserBtn.click(function(e) {
        searchData();
    });

    // 展示用户表格
    userTable = $("#user_table").myDataTable({
        url: "web/user/list.json",
        searchBtn: $searchUserBtn,
        sScrollY: "450px",
        sScrollX: true,
        sScrollXInner: "100%",
        aaSorting: [[5, "desc"]],
        aLengthMenu: [10, 30, 50, 100],
        aoColumns: [{
            mData: "checkbox",
            sTitle: "<label class='pos-rel' title='全选'><input type='checkbox' class='ace'><span class='lbl'></span></label>",
            sWidth: "50px",
            sClass: "center",
            bSortable: false,
            bSearchable: false,
            sDefaultContent: "",
            fnCreatedCell: function (cell, cellData, rowData, row, col) {
                var $cell = $(cell);

                $cell.html("<label class='pos-rel'><input type='checkbox' class='ace' data-id='{0}'><span class='lbl'></span></label>".format(rowData.id));
            }
        }, {
            mData: "loginName",
            sName: "loginName",
            sTitle: "姓名",
            sWidth: "200px"
        }, {
            mData: "loginid",
            sName: "loginid",
            sTitle: "账号",
            sWidth: "200px"
        }, {
            mData: "industryCode",
            sName: "industryCode",
            sTitle: "所属行业",
            sDefaultContent: "",
            fnCreatedCell: function (cell, cellData, rowData, row, col) {
                var $cell = $(cell);

                if (rowData.industryCode == "A") {
                    $cell.html("煤矿");
                } else if (rowData.industryCode == "B") {
                    $cell.html("非煤矿山");
                } else if (rowData.industryCode == "C") {
                    $cell.html("危化烟花爆竹");
                } else if (rowData.industryCode == "D") {
                    $cell.html("职业健康和工贸");
                } else if (rowData.industryCode == "E") {
                    $cell.html("公共");
                } else if (rowData.industryCode == "F") {
                    $cell.html("八大行业");
                } else if (rowData.industryCode == "X") {
                    $cell.html("其他");
                } else {
                    $cell.html(rowData.industryCode);
                }
            }
        }, {
            mData: "collieryNo",
            sName: "collieryNo",
            sTitle: "所属企业编码",
            sWidth: "200px"
        }, {
            mData: "isAdmin",
            sName: "isAdmin",
            sTitle: "用户类型",
            sWidth: "200px",
            sDefaultContent: "",
            fnCreatedCell: function (cell, cellData, rowData, row, col) {
                var $cell = $(cell);

                // 超级管理员
                if (rowData.isAdmin == "2") {
                    $cell.html("<span class='label label-lg label-danger'>超级管理员</span>");

                    // 管理员
                } else if (rowData.isAdmin == "1") {
                    $cell.html("<span class='label label-lg label-warning'>管理员</span>");

                    // 普通用户
                } else if (rowData.isAdmin == "0") {
                    $cell.html("<span class='label label-lg label-primary'>普通用户</span>");

                    // 其他
                } else {
                    $cell.html(rowData.isAdmin);
                }
            }
        }, {
            mData: "isLeader",
            sName: "isLeader",
            sTitle: "是否为领导",
            sWidth: "200px",
            sDefaultContent: "",
            fnCreatedCell: function (cell, cellData, rowData, row, col) {
                var $cell = $(cell);

                // 是
                if (rowData.isLeader == "1") {
                    $cell.html("<span class='label label-lg label-success'>是</span>");

                    // 否
                } else if (rowData.isLeader == "0") {
                    $cell.html("<span class='label label-lg label-danger'>否</span>");
                }
            }
        }, {
            mData: "operator",
            sTitle: "操作",
            sWidth: "100px",
            bSortable: false,
            bSearchable: false,
            sDefaultContent: "<div class='hidden-sm hidden-xs action-buttons'></div>",
            fnCreatedCell: function (cell, cellData, rowData, row, col) {
                // 单元格的 jQuery 对象
                var $cell = $(cell);

                // 超级管理员不能修改信息
                if ((globalLoginid != "administrator") && (globalLoginid == rowData.loginid)) {
                    $cell.find("div.action-buttons").append("<a class='danger-color' title='修改' href='{0}web/user/my.html?menuId=4502&menuParentId=450'><i class='ace-icon iconfont icon-pencil bigger-200'></i></a>".format(globalBasePath));
                }
            }
        }]
    });

    /**
     *
     * 搜索数据。
     *
     */
    function searchData() {
        // 用户
        if ($.isNotBlank($loginName.val()) && ($loginName.val().length > 50)) {
            $.toast({
                top: 5,
                msg: "姓名的长度不能大于50个字",
                type: "warn",
                close: 3000
            });

            return;
        }

        $loginName.val($.trim($loginName.val()));

        // 搜索开始…
        if ($.isNotNull(userTable)) {
            userTable.myAjaxData = {
                isAdmin: $isAdmin.val(),
                isLeader: $isLeader.val(),
                loginName: $loginName.val(),
                industryCode: $industryCode.val()
            };

            // 刷新用户表格（分页重置）
            userTable.ajax.reload();
        }
    }
});